import { message } from "antd";
import axios from "axios";
import history from "./history";
export const baseURL = 'http://geek.itheima.net'
const api = axios.create({
    baseURL: baseURL,
    timeout: 5000
})
api.interceptors.request.use(
    (request) => {
        if (localStorage.getItem('token')) {
            request.headers.Authorization = 'Bearer ' + localStorage.getItem('token')
        }
        return request
    },
    (error) => {
        return Promise.reject(error.message)
    }
)
api.interceptors.response.use(
    (response) => {
        message.success(response.data.message)
        return response.data
    },
    (error) => {
        console.dir(error.response);
        if (error.response) {

        } else {
            history.push('/')
            message.error('网络连接失败')
        }
        return Promise.reject(error.message)
    }
)
export default api